package com.netstar.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.widget.Toast;

import com.netstar.R;
import com.netstar.process.ProcessWatcher;
import com.netstar.service.NetService;
import com.netstar.util.EvtLog;
import com.netstar.util.FileUtil;

public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener {

	private static final String TAG = "SettingsActivity";
	private static final int DELETE_FILE_FAIL = 0;
	private static final int DELETE_FILE_SUCCESS = 1;
	private Resources mResources;
	private CheckBoxPreference mShowStatusbarPreference;
	private ProgressDialog mProgressDialog;

	Handler mHadler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (null != mProgressDialog && mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();
			}
			switch (msg.what) {
				case DELETE_FILE_FAIL:
					showToast("删除文件失败，请重试");
					break;
				case DELETE_FILE_SUCCESS:
					showToast("删除文件成功");
					break;
				default:
					break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		EvtLog.d(TAG, "onCreate");
		addPreferencesFromResource(R.xml.netstar_settings);

		initVariables();
		initViews();
		setListener();
	}

	private void setListener() {
		mShowStatusbarPreference.setOnPreferenceChangeListener(this);
	}

	private void initViews() {
		mShowStatusbarPreference = (CheckBoxPreference) findPreference(mResources
				.getText(R.string.key_show_in_statusbar));
	}

	private void initVariables() {
		mResources = getResources();
	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		if (preference == mShowStatusbarPreference) {
			Intent intent = new Intent(NetService.EVENT_SHOW_IN_STATUSBAR_CHANGED);
			SharedPreferences sp = getPreferenceManager().getSharedPreferences();
			intent.putExtra("show", !sp.getBoolean(getString(R.string.key_show_in_statusbar), true));
			sendBroadcast(intent);
		}
		return true;
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
		String key = preference.getKey();
		if (key != null) {
			if (key.equals(mResources.getText(R.string.key_clear))) {
				if (null == mProgressDialog) {
					mProgressDialog = new ProgressDialog(this);
				}
				mProgressDialog.setMessage("正在清除安装包");
				if (null != mProgressDialog && !mProgressDialog.isShowing()) {
					mProgressDialog.show();
				}
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							deleteAllFile();
							mHadler.sendEmptyMessage(DELETE_FILE_SUCCESS);
						} catch (Exception e) {
							mHadler.sendEmptyMessage(DELETE_FILE_FAIL);
							EvtLog.d(TAG, e.toString());
							return;
						}
					}
				}).start();
			}
		}
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}

	/**
	 * 
	 * @Title: deleteAllFile
	 * @Description: 删除所有下载的文件
	 * @param 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	private void deleteAllFile() throws Exception {
		EvtLog.d(TAG, "删除文件");
		FileUtil.deleteDir(FileUtil.FILE_PATH);
	}
}
